package com.example.day11matrixphotoview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ImageView;

@SuppressLint("AppCompatCustomView")
public class MatrixImage extends ImageView {
    private Matrix matrix;
    private Bitmap bitmap;
    private Paint paint;
    public MatrixImage(Context context) {
        super(context);
        init();
    }

    public MatrixImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    //必须元素
    private void init() {
        matrix =new Matrix();
        bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xz);
        paint=new Paint();
    }

    //平移
    public void translate(){
        matrix.reset();
        matrix.preTranslate(10,10);
        invalidate();   //无效
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,matrix,paint);
    }

    //旋转
    public void  rotate(){
        matrix.preRotate(10,bitmap.getWidth()>>1,bitmap.getHeight()>>1);
        invalidate();
    }

    //缩放
    public void scale(){
        //sx放大倍数 ,sy原尺寸
        matrix.preScale(1.1f,1,0,0);
        invalidate();
    }

    //错切
    public void skew(){
        matrix.preSkew(0,-0.1f,bitmap.getWidth()>>1,bitmap.getHeight()>>1);
        invalidate();
    }

}
